﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio2013.Modelo;
using System.Data;


namespace Obligatorio2013.Pages.Private
{
    public partial class Price : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if ((bool)Session["logged"] == true)
            {
                if (!Page.IsPostBack)
                {
                    if (Session["controller"] == null)
                    {
                        Session["controller"] = Sistema.Instance;
                    }

                    loadGrid();
                }
            }
            else
            {
                Response.Redirect("~/Pages/Private/LoginForm.aspx");
            }
        }

        protected void BtnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Public/Default.aspx", true);
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            Sistema system = Sistema.Instance;
            bool ret = system.savePrice(Double.Parse(this.txtPrecioPalabra.Text), Double.Parse(this.txtPorcentaje.Text));
            if (ret)
            {
                Response.Redirect("~/Pages/Public/Default.aspx", true);
            }
        }

        private void cleanForm()
        {
            this.txtPrecioPalabra.Text = "";
            this.txtPorcentaje.Text = "";
            loadGrid();
        }

        private void loadGrid() 
        {
            Sistema controller = Session["controller"] as Sistema;
            List<Obligatorio2013.Clases.Price> prices = controller.getPrices();
            if (prices != null && prices.Count != 0)
            {
                this.gvPriceList.DataSource = prices;
                this.gvPriceList.DataBind();
            }
            else
            {
                this.lblMessage.Text = "No hay Precios registrados en el sistema.";
            }
        }



        protected void gvPriceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPriceList.PageIndex = e.NewPageIndex;
            gvPriceList.DataBind();
        }

        protected void gvPriceList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = gvPriceList.DataSource as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                //dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvPriceList.DataSource = dataView;
                gvPriceList.DataBind();
            }
        }
    }
}